 back = imread('background19.jpg');
 fore = imread('foreground0.jpg');
 
  img_orig = fore;
  img_orig2 = back;
    %img_orig = img_orig -25;
    
%     h = fspecial('gaussian', [5 5]);
%     img_orig(:,:,1) = filter2(h, img_orig(:,:,1));
%     img_orig(:,:,2) = filter2(h, img_orig(:,:,2));
%     img_orig(:,:,3) = filter2(h, img_orig(:,:,3));
   %result(:,:,:) = back(:,:,:) - fore(:,:,:);
    
    height = size(img_orig,1);
    width = size(img_orig,2);
    
    %Initialize the output images
    out = img_orig;
    bin = zeros(height,width);
    a = zeros(height,width,3);
    
     out2 = img_orig;
    bin2 = zeros(height,width);
    a2 = zeros(height,width,3);
    
    %Apply Grayworld Algorithm for illumination compensation
    img = grayworld(img_orig);
    img2 = grayworld(img_orig2); 
    
    %Convert the image from RGB to YCbCr
   
    img_ycbcr = rgb2ycbcr(img);
    Cb = img_ycbcr(:,:,2);
    Cr = img_ycbcr(:,:,3);
    img_ycbcr(:,:,1)=100;
   
    
    img_ycbcr2 = rgb2ycbcr(img2);
    Cb2 = img_ycbcr(:,:,2);
    Cr2 = img_ycbcr(:,:,3);
    img_ycbcr(:,:,1)=100;
    
    
   result = img_ycbcr2;
   result(:,:,2)=Cb2-Cb;
   result(:,:,3)=Cr2-Cr;
  result = imsubtract(result,20);
 result = NegPic(result);
 result = im2bw(result, 0.5);
figure; imshow(result); title('Output');
    
    
%      for i=1:480
%         for j=1:640
%            if(lum2(i,j)==lum(i,j))
%                fore(i,j,1)=255;
%                fore(i,j,2)=255;
%                fore(i,j,3)=255;
%            end
%                
%         end
%      end
%     
%     img_ycbcr2(:,:,1)=lum(:,:);
%     figure;imshow(img_ycbcr2);
   
    
    %Detect Skin
     %[r,c,v] = find(Cb>=77 & Cb<=127 & Cr>=133 & Cr<=173); 
    %[r,c,v] = find( (Cb>=77 & Cb<=127 & Cr>=128 & Cr<=173 & lum>75) | lum >600);
    %numind = size(r,1);
    
    %Mark Skin Pixels
    %for i=1:numind
     %   a(r(i),c(i),:) = [0 0 255];   
     %   out(r(i),c(i),:) = [0 0 255];
    %    bin(r(i),c(i)) = 1;
   % end
    
    %figure; imshow(img_ycbcr);
    %figure; imshow(img_ycbcr2);
    
    
    %result = img_ycbcr2 - img_ycbcr;
    
    %figure;imshow(result);
 
% figure;imshow(back);

% a = imread('background0.jpg');
% b = imread('foreground0.jpg');
% 
% back = imadjust(a,[.2 .3 0; .6 .7 1],[]);
% figure, imshow(back);
% 
% 
% fore = imadjust(b,[.2 .3 0; .6 .7 1],[]);
% figure, imshow(fore);
% %fore = imdilate(fore,strel('disk',1));
% figure, imshow(fore);


%fore = histeq(b,imhist(a));
%fore = histeq(b,imhist(a));
%back = a;

 
%  result = back(:,:,:) - fore(:,:,:);
%  result = imsubtract(result,20);
%  result = NegPic(result);
%  result = im2bw(result, 0.5);
%  RCol=zeros(480,3);
%  fore2 = rgb2gray(fore);
%  figure, imshow(result);
% 
% for i=1:480
%     for j=1:640 
%         if result(i,j)==1
%             fore2(i,j)=255;           
%         end
%     end
% end
% 
% a=1;
% for i=1:480
%     for j=1:640 
%             if fore2(i,j)~=255
%                 RCol(a,1)=i;
%                 RCol(a,2)=j;
%                 break;
%             end    
%     end
%     for k=640:-1:1
%          if fore2(i,k)~=255
%                 RCol(a,3)=k;
%                 break;
%          end
%     end
%     a=a+1;
% end
% 
% 
% for i=1:480
% for j=1:640
%     if j<RCol(i,2)||j>RCol(i,3) 
%     fore(i,j,1)=255;
%     fore(i,j,2)=255;
%     fore(i,j,3)=255;
%     
%     end
% end
% 
% end
% fore = imdilate(fore,strel('disk',1));
% [x y z]=size(fore);
% if z==1
%     rslt=edge(fore,'canny');
% elseif z==3
%     img1=rgb2ycbcr(fore);
%     dx1=edge(img1(:,:,1),'canny');
%     dx1=(dx1*255);
%     img2(:,:,1)=dx1;
%     img2(:,:,2)=img1(:,:,2);
%     img2(:,:,3)=img1(:,:,3);
%     rslt=ycbcr2rgb(uint8(img2));
% end
% 
% R=rslt;
% imshow(R);
% fore = imfill(fore,'holes');
% figure;imshow(fore);
% 
% for i=1:480
%     if RCol(i,1)~=0
%    fore(RCol(i,1),RCol(i,2),1)=255;
%    fore(RCol(i,1),RCol(i,2),2)=0;
%    fore(RCol(i,1),RCol(i,2),3)=0;
%    fore(RCol(i,1),RCol(i,3),1)=255;
%    fore(RCol(i,1),RCol(i,3),2)=0;
%    fore(RCol(i,1),RCol(i,3),3)=0;
%     end
%    
% end
% figure;imshow(fore);
% 
% t=0;
% for i=1:(480-3)
%     for j=1:(640-3)
%         for k=1:3
%             for g=1:3
%                 if t==0
%                 if(fore(i+k,j+g,1)==255&&fore(i+k,j+g,2)==0 &&fore(i+k,j+g,3)==0)
%                     t = 1;
%                     if t==1
%                       fore(i+k,j+g,1)=255;
%                       fore(i+k,j+g,2)=0;
%                       fore(i+k,j+g,3)=0;  
%                     end
%                 end
%                 end               
%             end
%             t=0;
%         end
%       
%     end
% end
% 
% 
% figure;imshow(fore);
% 
